package com.shisfish.library.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.shisfish.library.Impl.BookImpl;
import com.shisfish.library.Impl.BookShelfImpl;
import com.shisfish.library.Model.Book;
import com.shisfish.library.Model.BookShelf;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("bookShelf")
public class BookShelfController extends GlobalVariable {

	private BookShelfImpl bookShelfImpl = new BookShelfImpl();
	
	private BookImpl bookImpl = new BookImpl();
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月8日 下午3:21:29
	 * @Descrition : 收藏一本书
	 * @param employee
	 * @param isbn
	 * @param request
	 * @return
	 */
	@POST
	@Path("collectABook/{employee}")
	@Produces("application/json; charset=utf-8")
	public String collectABook(@PathParam("employee") String employee, @FormParam("isbn") String isbn,
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginUser(employee, request)) {
			
			List<Book> books = bookImpl.selectByKey(isbn);
			
			if (books.size() > 0) {
				BookShelf bookShelf = new BookShelf(isbn, employee);
				
				int flag = bookShelfImpl.insertOne(bookShelf);
				
				if (flag == 1) {
					globalMap.put(STATUS_CODE, SUCCESS);
				} else {
					globalMap.put(STATUS_CODE, INSERT_WR);
				}
			} else if (books.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月8日 下午3:26:25
	 * @Descrition : 我的书架（分页）
	 * @param employee
	 * @param request
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@GET
	@Path("myBookShelf/{employee}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String myBookShelf(@PathParam("employee") String employee, @Context HttpServletRequest request, 
			@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginUser(employee, request)) {
			
			int count = bookShelfImpl.countByEmployee(employee);
			
			if (count > 0) {
				Page page = PageUtils.createPage(count, currentPage, pageSize);
				List<Map<String, Object>> bookShelfs = bookShelfImpl.selectByEmployeePage(employee, page.getBeginIndex(), pageSize);
				
				if (bookShelfs.size() > 0) {
					globalMap.put(STATUS_CODE, SUCCESS);
					globalMap.put("page", page);
					globalMap.put("list", bookShelfs);
				} else if (bookShelfs.size() == 0) {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				} else {
					globalMap.put(STATUS_CODE, SELECT_WR);
				}
				
			} else if (count == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月8日 下午4:19:30
	 * @Descrition : 取消收藏
	 * @param employee
	 * @param isbn
	 * @param request
	 * @return
	 */
	@POST
	@Path("unCollectABook/{employee}")
	@Produces("application/json; charset=utf-8")
	public String unCollectABook(@PathParam("employee") String employee, @FormParam("isbn") String isbn,
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginUser(employee, request)) {
			
			int flag = bookShelfImpl.deleteOneByKey(isbn);
			
			if (flag == 1) {
				globalMap.put(STATUS_CODE, SUCCESS);
			} else {
				globalMap.put(STATUS_CODE, DELETE_WR);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("maxCollect")
	@Produces("application/json; charset=utf-8")
	public String maxCollect() {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<Map<String, Object>> books = bookShelfImpl.maxCollect();
		
		if (books.size() > 0) {
			globalMap.put("list", books);
			globalMap.put(STATUS_CODE, SUCCESS);
		} else if (books.size() == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
		
	}
	
}
